package edu.gatech.volt2.drdoommgt.gui;

import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextArea;


@SuppressWarnings("serial")
public class LoginWindow extends JFrame implements ILoginView {

	private JPanel contentPane;
	private JButton submitButton;
	private JButton cancelButton;
	private JTextField emailAddress;
	private JPasswordField passwordField;
	private JLabel messageLabel;
	private JLabel lblDrDoomManagement;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginWindow frame = new LoginWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginWindow() {
		setTitle("Dr. Doom Management");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 392, 265);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(5, 11, 381, 228);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel logInLabel = new JLabel("Log In");
		logInLabel.setHorizontalAlignment(SwingConstants.CENTER);
		logInLabel.setBounds(150, 33, 70, 25);
		panel.add(logInLabel);
		
		JLabel emailLabel = new JLabel("Email: ");
		emailLabel.setHorizontalAlignment(SwingConstants.LEFT);
		emailLabel.setBounds(10, 70, 75, 30);
		panel.add(emailLabel);
		
		emailAddress = new JTextField();
		emailLabel.setLabelFor(emailAddress);
		emailAddress.setBounds(85, 70, 280, 30);
		panel.add(emailAddress);
		emailAddress.setColumns(10);
		
		JLabel passwordLabel = new JLabel("Password: ");
		passwordLabel.setBounds(10, 111, 75, 30);
		panel.add(passwordLabel);
		
		passwordField = new JPasswordField();
		passwordLabel.setLabelFor(passwordField);
		passwordField.setBounds(85, 111, 280, 30);
		panel.add(passwordField);
		
		submitButton = new JButton("Submit");
		submitButton.setBounds(275, 185, 90, 25);
		panel.add(submitButton);
		
		cancelButton = new JButton("Cancel");
		cancelButton.setBounds(10, 185, 90, 25);
		panel.add(cancelButton);
		
		messageLabel = new JLabel("");
		messageLabel.setForeground(Color.RED);
		messageLabel.setBounds(85, 142, 280, 31);
		panel.add(messageLabel);
		
		lblDrDoomManagement = new JLabel("Dr. Doom Management");
		lblDrDoomManagement.setHorizontalAlignment(SwingConstants.CENTER);
		lblDrDoomManagement.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		lblDrDoomManagement.setBounds(6, 6, 369, 16);
		panel.add(lblDrDoomManagement);
		
		KeyListener enterSubmits = new KeyListener() {
			@Override
			public void keyPressed(KeyEvent arg0) {}
			@Override
			public void keyReleased(KeyEvent arg0) {
				System.out.println(arg0.getKeyCode());
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER){
					submitButton.doClick();
				}
			}
			@Override
			public void keyTyped(KeyEvent arg0) {}
		};
		passwordField.addKeyListener(enterSubmits);
	}

	public void addListener(ActionListener al) {
		cancelButton.addActionListener(al);
		submitButton.addActionListener(al);
	}

	public void showSelf(){
		this.setVisible(true);
	}
	
	public void hideSelf(){
		this.setVisible(false);
	}
	public void closeSelf(){
		System.exit(0); //Only this for the login window... to kill the program
	}
	
	public JButton getSubmitButton(){
		return submitButton;
	}
	
	public JButton getCancelButton(){
		return cancelButton;
	}

	public JTextField getEmailTextField(){
    	return emailAddress;
    }
    public JPasswordField getPasswordField(){
    	return passwordField;
    }

	@Override
	public void setErrorMessage(String message) {
		messageLabel.setText(message);
	}

	@Override
	public void clearFields() {
		passwordField.setText("");
		emailAddress.setText("");
	}
	
	public String getEmail(){
		return emailAddress.getText();
	}
	
	public String getPassword(){
		char[] pass = passwordField.getPassword();
		String result = "";
		for (char c : pass ){
			result+= Character.toString(c);
		}
		return result;
	}
}
